package com.woniu.woniuproject.config;

import com.alibaba.fastjson.JSONObject;
import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * 功能描述:<br>
 * 〈〉
 *
 * @author EDZ
 * @create 2020/11/18
 * @since 1.0.0
 */
@Service
public class SendSms {
    /*
    phoneNum  手机号
    TemplateCode  模板名称
    code  验证码
    * */
    public Map<String, Object> send(String phoneNum, String TemplateCode, HashMap<String, Object> code) {
        DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", "LTAI4GGpavCdBarX3ntxmAQg", "CDJNtYE6JjkkNSS8ArSiMOT3nobHBH");
        IAcsClient client = new DefaultAcsClient(profile);
        CommonRequest request = new CommonRequest();
        request.setSysMethod(MethodType.POST);
        request.setSysDomain("dysmsapi.aliyuncs.com");
        request.setSysVersion("2017-05-25");
        request.setSysAction("SendSms");

        request.putQueryParameter("RegionId", "cn-hangzhou");
        request.putQueryParameter("PhoneNumbers", phoneNum);
        request.putQueryParameter("SignName", "ABC商城");
        request.putQueryParameter("TemplateCode", TemplateCode);
        Map<String, Object> map = new HashMap<>();
        request.putQueryParameter("TemplateParam", JSONObject.toJSONString(code));
        try {

            CommonResponse response = client.getCommonResponse(request);
            map.put("message",response.getData());
            System.out.println(response.getData());
            map.put("boolean",response.getHttpResponse().isSuccess());
            //返回是否成功
            return map;
        } catch (ClientException e) {
            e.printStackTrace();
        }
        map.put("boolean",false);
        return map;
    }
}
